package cn.fl.ass.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.ass.service.IAssSchFeeDService;
import cn.fl.ass.vo.AssParkingVO;
import cn.fl.ass.vo.AssSchFeeDVO;
import cn.fl.capital.condition.CapPayInfoQueryCondition;
import cn.fl.capital.constant.CapPayInfoConstant;
import cn.fl.capital.vo.CapPayInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjPrdPaySchDVO;
import cn.fl.project.vo.PrjPrdPaySchMVO;
import cn.fl.system.vo.SysUlFilesRecVO;
import fl.ass.facade.condition.AssParkingQueryCondition;
import fl.ass.facade.dto.MessageDTO;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;


/**
 * @Description: 追加费用 控制层
 */
@Controller
@RequestMapping("/ass/assSchFee")
public class AssSchFeeDController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AssSchFeeDController.class);

	/**
	 * AssParking 停车场服务层BEAN
	 */
	@Autowired
	private IAssSchFeeDService assSchFeeDService ;
	
	/**
	 * 到新增页面
	 */
	@RequestMapping(value = "/toAssSchFeePage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAssSchFeePage(HttpServletRequest request, HttpServletResponse response) {
		String paySchId = request.getParameter("paySchId");
		String paySchNo = request.getParameter("paySchNo");
		ModelAndView view = new ModelAndView("ass/assSchFee/assSchFeePage");
		view.addObject("paySchId", paySchId);
		view.addObject("paySchNo", paySchNo);
		return view;
	}
	
	/**
	 * 到添加附件页面
	 */
	@RequestMapping(value = "/toAddfileResPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView addfileResPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("ass/assSchFee/assSchFeeDUploadFilesPage");
		view.addObject("id", id);
		return view;
	}
	
	
	/**
     * 查看添加费用项 明细
     */
    @RequestMapping(value = "/toAssSchFeeDDetailPage.do", method = {RequestMethod.GET})
    public ModelAndView toRickDepDetailPage(HttpServletRequest request, HttpServletResponse response) {
        String id =request.getParameter("id");
        ModelAndView mav = new ModelAndView("ass/assSchFee/assSchFeeDDetailPage");
        mav.addObject("id", id);
        return mav;
    }
	
    /**
	 * 到查看附件详情页面
	 */
	@RequestMapping(value = "/toCheckfilesPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toCheckfilesPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");// ass_sch_fee_d 的主键
		ModelAndView view = new ModelAndView("ass/assSchFee/assSchFeeFilesDetailPage");
		view.addObject("id", id);
		return view;
	}
	
    
    
	/**
	 * 添加支付表费用项明细
	 */
	@RequestMapping(value = "/saveAssSchFeeD.do", method = { RequestMethod.POST})
	public @ResponseBody Map<String ,Object> initList(@RequestBody Object formdata) {
		Map<String ,Object> map = new HashMap<String ,Object>();
		Map<String ,Object> paramMap = new HashMap<String ,Object>();
		List<AssSchFeeDVO> assSchFeeVoList = new ArrayList<AssSchFeeDVO>();
		MessageDTO<String> response = new MessageDTO<String>();
		response.setMessage("发起流程失败!");
		AssSchFeeDVO assSchFeeVo = null;
		JSONObject object = new JSONObject();
		Long paySchId = (long) -1;
		String paySchNo = "";
		if(formdata != null){
			object = JSONObject.parseObject(formdata.toString());
			paySchId = object.getLong("paySchId");
			paySchNo = object.getString("paySchNo");
			JSONArray gridDate = object.getJSONArray("gradeDate");
			JSONArray docArray = object.getJSONArray("docArray");
			paramMap.put("paySchId", paySchId);
			paramMap.put("paySchNo", paySchNo);
			try {
				for(int i=0;i<gridDate.size();i++){
					JSONObject objTmp = gridDate.getJSONObject(i);
					assSchFeeVo = new AssSchFeeDVO();
					List<SysUlFilesRecVO> fileList = new ArrayList<SysUlFilesRecVO>();
					String id = objTmp.getString("id");
					assSchFeeVo.setId(objTmp.getLong("id"));
					assSchFeeVo.setFeeTypeCd(objTmp.getString("feeTypeCd"));
					assSchFeeVo.setApplyAmt(objTmp.getLong("applyAmt"));
					assSchFeeVo.setPayeeAccNo(objTmp.getString("payeeAccNo"));
					assSchFeeVo.setPayeeAccNm(objTmp.getString("payeeAccNm"));
					assSchFeeVo.setFeeDesc(objTmp.getString("feeDesc"));
					assSchFeeVo.setPayeeBankNo(objTmp.getString("payeeBankNo"));
					assSchFeeVo.setPayeeBankNm(objTmp.getString("payeeBankNm"));
					assSchFeeVo.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
					assSchFeeVo.setCrtTm(new Date());
					assSchFeeVo.setIsDel(0);//0 正常
					for(int k=0;k<docArray.size();k++){
						JSONObject docfile = docArray.getJSONObject(k);
	            	    String fileParams = docfile.getString(id);
		    			fileList = JSON.parseArray(fileParams, SysUlFilesRecVO.class);
		    			for (SysUlFilesRecVO sysUlFilesRecVO : fileList) {
		    				sysUlFilesRecVO.setSysCd(CurrentThreadContext.getCurrentSysCd());
		        			sysUlFilesRecVO.setOrgCd(PrjBscInfoConstant.CODE_PRJ_FILE_DEF_ORG);
		    			}
		    			assSchFeeVo.setFileList(fileList);
					}
					assSchFeeVoList.add(assSchFeeVo);
				}
				 response = assSchFeeDService.saveAssSchFeeD(paramMap,assSchFeeVoList);
			} catch (Exception e) {
				map.put("result", e.getMessage());
				BaseLogUtils.newLogger("发起追加费用流程异常").setThrowable(e).info();
			}
		}
		BaseLogUtils.newLogger("发起追加费用流程").setKeys("结果:" + response.isSuccess()).info();
		if(response.isSuccess()){
			map.put("resultType", "1");
			map.put("resultMsg", response.getMessage());
            BaseLogUtils.newLogger("发起追加费用流程成功").info();
		}else{
			map.put("resultType", "0");
			map.put("resultMsg", response.getMessage());
            BaseLogUtils.newLogger("发起追加费用流程失败").setKeys("信息:" + response.getMessage()).info();
		}
		
		return map;
	}
	
	/**
	 * 添加支付表费用项明细
	 */
	@RequestMapping(value = "/getId.do", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Map<String ,Object> getId(HttpServletRequest request) {
		Map<String ,Object> map = new HashMap<String ,Object>();
		Long id = assSchFeeDService.getId();
		map.put("id", id);
		if(id==0){
		    BaseLogUtils.newLogger("生成明细id失败").info();
		}
        BaseLogUtils.newLogger("生成明细id完成").info();
		return map;
	}
	
	
	/**
	 * 初始化列表
	 */
	@RequestMapping(value = "/initAssSchFeeDList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initAssSchFeeDList(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		page.setCondition(id);
		Pager pager = this.assSchFeeDService.selectById(page);
        List<AssSchFeeDVO> list = (List<AssSchFeeDVO>) pager.getResultList();
		ListPageVO<AssSchFeeDVO> listPage = new ListPageVO<AssSchFeeDVO>(list, pager.getTotalCount());
		return listPage;
	}
	
	
}

